package com.xplusplus.security.service;

import com.xplusplus.security.domain.ImportUserMessage;
import com.xplusplus.security.repository.ImportUserMessageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: zhouweixin
 * @Description:
 * @Date: Created in 21:12 2018/10/24
 * @Modified By:
 */
@Service
public class ImportUserMessageService {
    @Autowired
    private ImportUserMessageRepository importUserMessageRepository;

    /**
     * 查询所有
     *
     * @return
     */
    public List<ImportUserMessage> getAll() {
        return importUserMessageRepository.findAll();
    }

    /**
     * 通过时间查询
     *
     * @param time
     * @return
     */
    public List<ImportUserMessage> getByTime(Date time) {
        return importUserMessageRepository.findByTime(time);
    }

    /**
     * 通过id删除
     *
     * @param id
     */
    public void deleteById(Long id) {
        importUserMessageRepository.delete(id);
    }

    /**
     * 删除所有
     */
    public void deleteAll() {
        importUserMessageRepository.deleteAll();
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Transactional
    public void deleteByIds(Long[] ids) {
        importUserMessageRepository.deleteByIdIn(Arrays.asList(ids));
    }

    /**
     * 通过状态和时间查询
     *
     * @param status
     * @param time
     * @return
     */
    public List<ImportUserMessage> getByStatusAndTime(String status, Date time) {
        return importUserMessageRepository.findByStatusAndTime(status, time);
    }

    /**
     * 查询最新的导入信息
     *
     * @return
     */
    public List<ImportUserMessage> getLastest() {
        ImportUserMessage importUserMessage = importUserMessageRepository.findFirstByOrderByIdDesc();
        if(importUserMessage == null || importUserMessage.getTime() == null){
            return null;
        }
        return importUserMessageRepository.findByTime(importUserMessage.getTime());
    }

    /**
     * 通过状态查询最新的
     *
     * @param status
     * @return
     */
    public List<ImportUserMessage> getLastestByStatus(String status) {
        ImportUserMessage importUserMessage = importUserMessageRepository.findFirstByOrderByIdDesc();
        if(importUserMessage == null || importUserMessage.getTime() == null){
            return null;
        }
        return importUserMessageRepository.findByStatusAndTime(status, importUserMessage.getTime());
    }
}
